package ${package.Controller};

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

import org.springframework.web.bind.annotation.RequestMapping;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.web.bind.annotation.RestController;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@RestController
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass}<${entity}, ${table.serviceName}> {

    private final String prefix="<#if package.ModuleName??>${package.ModuleName}/</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}/<#else>${table.entityPath}/</#if>";
    
	@Autowired
	${table.serviceName} ${table.serviceName?uncap_first};

	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody ${entity}  ${entity?uncap_first}){
		Query<${entity}> query = new Query<${entity}>(${entity?uncap_first});
		List<${entity}> ${entity?uncap_first}s=${table.serviceName?uncap_first}.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(${entity?uncap_first}s,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", ${table.serviceName?uncap_first}.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		${entity?uncap_first}.setCreateId(getUserId());
		if(${table.serviceName?uncap_first}.save(${entity?uncap_first})){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		${entity?uncap_first}.setModifiedId(getUserId());
		if(${table.serviceName?uncap_first}.update(${entity?uncap_first})){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		
		if(${table.serviceName?uncap_first}.deleteById(${entity?uncap_first}.get<#list table.fields as field><#if field.keyFlag><#-- 主键 -->${field.capitalName}</#if></#list>())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(${table.serviceName?uncap_first}.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}

}
<#else>
public class ${table.controllerName} {
  
    private final String prefix="<#if package.ModuleName??>${package.ModuleName}/</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}/<#else>${table.entityPath}/</#if>";
    
	@Autowired
	${table.serviceName} ${table.serviceName?uncap_first};

	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody ${entity}  ${entity?uncap_first}){
		Query<${entity}> query = new Query<${entity}>(${entity?uncap_first});
		List<${entity}> ${entity?uncap_first}s=${table.serviceName?uncap_first}.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(${entity?uncap_first}s,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", ${table.serviceName?uncap_first}.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		${entity?uncap_first}.setCreateId(getUserId());
		if(${table.serviceName?uncap_first}.save(${entity?uncap_first})){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		role.setModifiedId(getUserId());
		if(${table.serviceName?uncap_first}.update(${entity?uncap_first})){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) ${entity} ${entity?uncap_first},BindingResult bindingResult){
		if(${table.serviceName?uncap_first}.deleteById(${entity?uncap_first}.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("<#if package.ModuleName??>${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(${table.serviceName?uncap_first}.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
}
</#if>
</#if>
